<template>
    <div>
        <Title title="找回密码"></Title>
        <div class="subFrom">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="用户名" prop="name">
                    <el-input v-model="ruleForm.name" placeholder="请输入用户名" autocomplete="new-password"></el-input>
                </el-form-item>
                <!-- 新密码 -->
                <el-form-item label="新密码" prop="newpwd">
                    <el-input v-model="ruleForm.newpwd" placeholder="请输入密码" autocomplete="new-password"></el-input>
                </el-form-item>
                <!-- 手机号 -->
                <el-form-item label="手机号" prop="tel">
                    <el-input v-model="ruleForm.tel" placeholder="请输入手机号" autocomplete="new-password"></el-input>
                    <!-- <button class="yanzheng" @click.prevent="getCode">获取验证码</button> -->
                    <button class="yanzheng" @click.prevent="getCode" v-show='isbtn'>获取验证码</button>
                    <button class="yanzheng" @click.prevent="getCode" disabled v-show='!isbtn'>{{m}}:{{s}}</button>
                </el-form-item>
                <!-- 验证码 -->
                <el-form-item label="验证码" prop="code">
                    <el-input v-model="ruleForm.code" placeholder="请输入验证码" autocomplete="new-password"></el-input>
                </el-form-item>
                <!-- 提交按钮 -->
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">找回密码</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>
<script>
import Title from '../components/Title.vue' 
import {getYanZheng} from '../api/luntan-getmy.js' 
import {forgetPwd} from '../api/luntan-getmy.js'
export default {
    components:{
        Title
    },
    data() {
      return {
        ruleForm: {
          name: '',
          newpwd:"",
          tel:"",
          code:"",
        },
        isbtn:true,
        m:5,
      s:'00',
        rules: {
          name: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 1, max: 15, message: '长度在 1 到 15 个字符', trigger: 'blur' }
          ],
          newpwd: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 6, message: "密码长度至少6位", trigger: "blur" }
          ],
          tel: [
            { required: true, message: '请输入手机号', trigger: 'blur' },
            { min: 11, max: 11, message: '手机号格式错误', trigger: 'blur' }
          ],
          code: [
            { required: true, message: '请输入验证码', trigger: 'blur' },
            { min: 6, max: 6, message: '验证码格式错误', trigger: 'blur' }
          ]
        }
      };
    },
     methods: {
      open(msg) {
        this.$message({
          message: msg,
          type: 'warning'
        });
      },
        getCode(){
            if(Number(this.ruleForm.tel)){
                getYanZheng(Number(this.ruleForm.tel))
                this.isbtn = false
                let time = setInterval(() => {
                  if(this.s==0&&this.m!==0){
                    this.s=59;
                    this.m -=1 
                  }else if(this.m==0&&this.s==0){
                    this.isbtn = true
                    clearInterval(time)
                  }else if(this.s>=0&&this.s<=10){
                    this.s-=1
                    this.s = '0'+this.s
                  }else{
                    this.s-=1
                  }
                }, 1000);
            }else{
                this.open("请输入手机号")
            }
        },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            forgetPwd(this.ruleForm.name,this.ruleForm.newpwd,Number(this.ruleForm.tel),this.ruleForm.code).then(res=>{
                 // eslint-disable-next-line no-debugger
                debugger
                if(res.code =="500"){
              // 验证码错误
              this.open(res.msg)
            }else {
                // console.log("denglu");
              // 验证码正确，跳到登录页面
              this.$router.push({
                path:'/home/login'
              })
            }
            })
          } else {
            // console.log('error submit!!');
            this.open("请填写正确的信息")
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
}
</script>
<style lang="less" scoped>
    .subFrom {
        width: 80%;
        margin: 20px;
        .yanzheng {
            float: right;
            position: relative;
            top: 5px;
            margin-bottom: -10px;
            color: #666;

        }
    }
</style>